/*
* 公司：Leita Tech
* 版权信息：雷塔所有
* 任务：
* 描述：
* 作者：李大平
* 日期：2020/06/04
*
*/

#ifndef LT_LIST_MODEL_H
#define LT_LIST_MODEL_H

#include <QAbstractListModel>
#include <QDebug>
#include "model/LTModelConfig.h"

class MODEL_API LTListModel : public QAbstractListModel
{
    Q_OBJECT
public:
    typedef QHash<QString,QVariant> Data;
    LTListModel(const QHash<int, QByteArray> &role_names, QObject *parent = nullptr);
    LTListModel(const QVector<QByteArray> &role_names, QObject *parent = nullptr);
    int rowCount(const QModelIndex &parent) const override;
    QVariant data(const QModelIndex &index, int role) const override;
    QHash<int, QByteArray> roleNames() const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;

    Q_INVOKABLE bool deleteData(int index);
    void deleteDatas(std::function<bool(const Data&)> cond);
    Q_INVOKABLE int addData(const Data &data);
    Q_INVOKABLE void setData(int index,const QVariant &value, const QByteArray &role);
    Q_INVOKABLE void setData(int index,const Data &data);
    Q_INVOKABLE void clearDatas();
    const Data &getData(int index) const;
    int getData(const QByteArray &role, const QVariant &role_value);
    int makeData(const QByteArray &role, const QVariant &role_value);
    bool hasData(const QByteArray &role, const QVariant &role_value) const;
    Q_INVOKABLE QVariant getData(int _index,const QByteArray &role);
    const QVector<Data> &getDatas() const{ return datas_; }
    Q_INVOKABLE void sortDatas(const QByteArray &role, bool asscending = true);
    Q_INVOKABLE void stableSortDatas(const QByteArray &role, bool asscending = true);
    Q_INVOKABLE void insertData(QVariant ini_val);
private:
    QByteArray getRoleName(int role) const;
    QVector<Data> datas_;
    QHash<int, QByteArray> role_names_;
    QHash<QByteArray, int> role_indices_;
    static Data null_data_;
};

#endif // LT_LIST_MODEL_H
